<?php

class CategoryController extends Zend_Controller_Action 
{
    protected $_model;

	public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('view', 'html')
                    ->addActionContext('comment', 'html')                    
                    ->initContext();
    }
    
    public function indexAction()
    {
        $model = $this->_getModel();
        $this->view->categories = $model->fetchCategories();
    }
    public function allAction()
    {
    	$request = $this->getRequest();
    	$callback = $request->getParam('callback');
        $model = $this->_getModel();
		$sort = $request->getParam('sort');
		$dir = $request->getParam('dir');
        $start = $request->getParam('start');
        $limit = $request->getParam('limit');
   		$count = $total = $model->getTotal();
        $categories = $model->getCategories($sort, $dir, $start, $limit);
        $json = array();
        $json['totalCount'] = $count;
        $json['categories'] = $categories;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
    }
	
	public function byareaAction()
    {
    	$request = $this->getRequest();
    	$callback = $request->getParam('callback');
        $model = $this->_getModel();
		$sort = $request->getParam('sort');
		$dir = $request->getParam('dir');	
		$area_id = $request->getParam('area_id');
        $categories = $model->getByAreaId($area_id, $sort, $dir);
   		$count = count($categories);
        $json = array();
        $json['totalCount'] = $count;
        $json['categories'] = $categories->toArray();
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
    }
    public function areasAction(){    	
        $request = $this->getRequest();
    	$callback = $request->getParam('callback');
        $areas = $this->getAreas();
        $json = array();
        $json['totalCount'] = count($areas);
        $json['areas'] = $areas;
        echo $callback.'('.Zend_Json::encode($json).')';
        die();
    }
    public function viewAction()
    {
        $model = $this->_getModel();
        $request = $this->getRequest();
        $id = $request->getParam('id');
        $this->view->post = $model->fetchEntry($id);   
        $model = $this->getCommentModel();                 
        $this->view->areas = $model->fetchEntries();             
    }


	public function editAction()
    {
		$request = $this->getRequest();
		$data = $request->getParams();
		$model = $this->_getModel();
        $model->save($data);        
        die(); 
    }		
    public function deleteAction(){
    	$model = $this->_getModel();
      $request = $this->getRequest();
      $id = $request->getParam('id'); 
      $model->deleteCategory($id);
    	die();
    }    
  
    protected function _getModel()
    {
        if (null === $this->_model) {
            require_once APPLICATION_PATH . '/models/Category.php';
            $this->_model = new Model_Category();
        }
        return $this->_model;
    }
		private function getAreas(){
				require_once APPLICATION_PATH . '/models/Area.php';
      	$model = new Model_Area();
      	return $model->fetchEntries();
    } 
    private function getSelectItems(){
    	  $data = $this->getAreas();        
        $size = count($data);
        $areas = array();                
        for($i = 0; $i < $size; $i++){
        	$areas[$data[$i]['id']] = $data[$i]['name'];
        }
        return $areas;
    }
}
